package test.administrator.example.com.mvptest1.framework.support.activity;

/**
 * Created by thinkpad on 2018/7/18.
 */

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;

import java.util.UUID;

import test.administrator.example.com.mvptest1.framework.MvpPresenter;
import test.administrator.example.com.mvptest1.framework.MvpView;
import test.administrator.example.com.mvptest1.framework.support.MvpDelegateCallback;
import test.administrator.example.com.mvptest1.framework.support.ProxyMvpDelegateCallback;
import test.administrator.example.com.mvptest1.framework.support.manager.PresenterManager;

/**
 * 代理模式->第一重代理->生命周期->目标对象：实现生命周期方法（很多功能，例如：绑定和解绑、数据缓存、View状态缓存等等...）
 */
public class ActivityMvpDelegateImpl<V extends MvpView, P extends MvpPresenter<V>> implements ActivityMvpDelegate<V, P> {

	private ProxyMvpDelegateCallback<V, P> proxyMvpCallback;

	/**
	 * @param activity
	 * @param mvpCallback
	 * @param keepPresenterInstance 是否开启Presenter缓存
	 */
	public ActivityMvpDelegateImpl(Activity activity, MvpDelegateCallback<V, P> mvpCallback, boolean keepPresenterInstance) {
		//用于做P层缓存
		this.activity = activity;
		this.proxyMvpCallback = new ProxyMvpDelegateCallback<V, P>(mvpCallback);
		this.keepPresenterInstance = keepPresenterInstance; //表示是否创建新的对象
	}

	private Activity activity;
	protected boolean keepPresenterInstance;
	private static final String KEY_VIEW_ID = "test.administrator.example.com.mvptest1.framework.support.id";
	protected String viewId;

	private P getPresenter() {
		P presenter = proxyMvpCallback.getPresenter();
		if (presenter == null) {
			throw new NullPointerException("presenter不能为空！！！");
		}
		return presenter;
	}

	private V getMvpView() {
		V view = proxyMvpCallback.getMvpView();
		if (view == null) {
			throw new NullPointerException("view不能为空！！！");
		}
		return view;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		//1、原始代码
//		this.proxyMvpCallback.createPresenter();
//		this.proxyMvpCallback.attachView();

		//2、优化后代码->因为要缓存P
		//缓存P层
		P presenter = null;
		if (savedInstanceState != null && keepPresenterInstance) {
			viewId = savedInstanceState.getString(KEY_VIEW_ID);
			//说明存在
			if (viewId == null) {
				//创建P层
				presenter = createViewIdAndCreatePresenter();
			}
			//否则不处理
		} else {
			//直接创建->说明没有缓存
			presenter = createViewIdAndCreatePresenter();
		}
		//绑定P
		proxyMvpCallback.setPresenter(presenter);
		//绑定V层
		getPresenter().attachView(getMvpView());
	}

	/**
	 * 根据ViewId创建Presenter
	 *
	 * @return
	 */
	private P createViewIdAndCreatePresenter() {
		//缓存P
		P presenter = proxyMvpCallback.createPresenter();
		if (presenter == null) {
			//如果为空，说明客户端实现类没有创建P对象
			throw new NullPointerException("view不能为空！！！");
		}
		if (keepPresenterInstance) {
			//第一次进来的话都会创建
			viewId = UUID.randomUUID().toString();
			//缓存->PresenterManager
			PresenterManager.putPresenter(activity, viewId, presenter);
		}
		return presenter;
	}

	@Override
	public void onStart() {

	}

	@Override
	public void onRestart() {

	}

	@Override
	public void onResume() {

	}

	@Override
	public void onPause() {

	}

	@Override
	public void onStop() {

	}

	/**
	 * 是否存在这个实例（不存在没必要销毁->返回false，存在->返回true，需要销毁）
	 *
	 * @param keepPresenterInstance
	 * @param activity
	 * @return
	 */
	protected static boolean retainPresenterInstance(boolean keepPresenterInstance, Activity activity) {
		return keepPresenterInstance && //存在该Presenter实例
				(activity.isChangingConfigurations() //activity处于运行状态
						|| !activity.isFinishing()); //activity处于打开状态
	}

	@Override
	public void onPostCreate(@Nullable Bundle savedInstanceState) {

	}

	@Override
	public void onSaveInstanceState(Bundle outState) {

	}

	@Override
	public void onDestroy() {
		//原始代码
//		this.proxyMvpCallback.detachView();

		//优化代码
		boolean retainPresenterInstance = retainPresenterInstance(keepPresenterInstance, activity);
		getPresenter().detachView();

		//同时P层也需要销毁
		if (!retainPresenterInstance) {
			getPresenter().destroy();
		}

		if (!retainPresenterInstance && viewId != null) {
			PresenterManager.remove(activity, viewId);
		}
	}

}



























































































